﻿Imports agsXMPP
Public Class chatWindowsTabs
    Public Property name As Jid

    Private Sub chatWindowsTabs_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        TabControl1.Height = TabControl1.Parent.GetValue(HeightProperty)

    End Sub
    Public Sub recMesg(ByVal message As String, ByVal fromP As Jid, ByVal resource As String)
        '  If StackPanel1.Dispatcher.Thread.Equals(System.Threading.Thread.CurrentThread) Then

        '  Else
        '     StackPanel1.Dispatcher.BeginInvoke(New (AddressOf recMesg), New String() {message, fromP})
        ' Return
        ' End If
        If message = "" Then
            Exit Sub
        End If
        Dim m As New TextBlock()
        m.TextWrapping = TextWrapping.Wrap
        m.Width = LayoutRoot.ActualWidth - 2
        m.Text = fromP.User + " says, " + message
        StackPanel1.Children.Add(m)
        chatBox.Focus()
    End Sub

    Private Sub chatwindow_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        Dim m As Application = Application.Current()
        m.windowsOpen.Remove(Name.Bare)

    End Sub

    Private Sub chatwindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Dim m As Application = Application.Current()
        m.windowsOpen.Add(Name.Bare.ToLower())

    End Sub


    Private Sub chatBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles chatBox.KeyDown
        If e.Key = Key.Enter Then
            Dim m As New agsXMPP.protocol.client.Message()
            m.Type = protocol.client.MessageType.chat
            m.To = Name
            m.Body = chatBox.Text
            Dim mw As MainWindow = Application.Current.Windows.Item(0)
            mw.ags.Send(m)
            Dim mb As New TextBlock()
            mb.Text = "You say, " + chatBox.Text
            mb.TextWrapping = TextWrapping.Wrap
            StackPanel1.Children.Add(mb)
            chatBox.Text = ""
            chatBox.Focus()
        End If
    End Sub

End Class
